package com.sky.service.impl;

import com.sky.entity.User;
import com.sky.mapper.UserMapper;
import com.sky.mapper.WorkSpaceMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    WorkSpaceMapper workSpaceMapper;
    @Autowired
    UserMapper userMapper;




    @Override
    public BusinessDataVO businessData(LocalDateTime start,LocalDateTime end) {




        DecimalFormat df = new DecimalFormat("#0.00");
        int count =0;
        BusinessDataVO businessDataVO = new BusinessDataVO();
        Double totalMoney = workSpaceMapper.getTotalMoney(start,end);
        if (totalMoney!=null){
            String totalMoneyString = df.format(totalMoney);
            totalMoney = Double.valueOf(totalMoneyString);
        }else {
            totalMoney=0d;
        }


        businessDataVO.setTurnover(totalMoney);
        Integer validOrderCount= workSpaceMapper.getActiveOrdersNumber(start,end);
        //转成double类型
        Double dValidOrderCount = Double.valueOf(validOrderCount);
        businessDataVO.setValidOrderCount(validOrderCount);

        Integer totalOrders = workSpaceMapper.selectAllOrdersCount();
        Double dTotalOrders = Double.valueOf(totalOrders);

        Double value=0d;
        if (dTotalOrders != 0){
          value = dValidOrderCount/dTotalOrders;

        }
        businessDataVO.setOrderCompletionRate(value);

        Double DoublePrice=0d;
        if (validOrderCount!=0){
          Double  unitPrice = totalMoney/validOrderCount;
            String price = df.format(unitPrice);
             DoublePrice = Double.valueOf(price);
        }
        businessDataVO.setUnitPrice( DoublePrice);
        LocalDate today = LocalDate.now();
        String todayTime = String.valueOf(today);
        Integer newUsers=userMapper.countNews(start,end);

        businessDataVO.setNewUsers(newUsers);
        return businessDataVO;
    }

    @Override
    public SetmealOverViewVO getoverviewSetmeals() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();

        Integer sold =  workSpaceMapper.count(1);
        Integer discontinued =workSpaceMapper.count(0);
        setmealOverViewVO.setSold(sold);
        setmealOverViewVO.setDiscontinued(discontinued);
        return setmealOverViewVO;
    }


    @Override
    public DishOverViewVO getOverviewDishes() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        Integer sold=  workSpaceMapper.countDishStatus(1);
        Integer discontinued =  workSpaceMapper.countDishStatus(0);
        dishOverViewVO.setSold(sold);
        dishOverViewVO.setDiscontinued(discontinued);
        return dishOverViewVO;
    }



    @Override
    public OrderOverViewVO getOverviewOrders() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();

        Integer waitingOrders  = workSpaceMapper.getOverviewOrders(2);
        Integer deliveredOrders  = workSpaceMapper.getOverviewOrders(3);
        Integer completedOrders  = workSpaceMapper.getOverviewOrders(5);
        Integer cancelledOrders  = workSpaceMapper.getOverviewOrders(6);
        Integer allOrders  = workSpaceMapper.getOverviewOrders(null);

        orderOverViewVO.setAllOrders(allOrders);
        orderOverViewVO.setCancelledOrders(cancelledOrders);
        orderOverViewVO.setCompletedOrders(completedOrders);
        orderOverViewVO.setDeliveredOrders(deliveredOrders);
        orderOverViewVO.setWaitingOrders(waitingOrders);
        return orderOverViewVO;
    }
}
